package com.a3dweijing.wetouch2.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.a3dweijing.wetouch2.Common.CallBack;
import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.network.RetrofitUtil;
import com.a3dweijing.wetouch2.adapter.EvaluatedListAdapter;
import com.a3dweijing.wetouch2.base.BaseActivity;
import com.a3dweijing.wetouch2.bean.EvaluatedDate;
import com.a3dweijing.wetouch2.bean.OrderListBean;
import com.a3dweijing.wetouch2.datasave.Singleton;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class EvaluatedListActivity extends BaseActivity
{
    
    @BindView(R.id.back)
    ImageView back;
    
    @BindView(R.id.top_text)
    TextView topText;
    
    @BindView(R.id.top_right)
    TextView topRight;
    
    @BindView(R.id.top_all)
    RelativeLayout topAll;
    
    @BindView(R.id.progressBar)
    ProgressBar progressBar;
    
    @BindView(R.id.loading_date)
    LinearLayout loadingDate;
    
    @BindView(R.id.resaut_status_img)
    ImageView resautStatusImg;
    
    @BindView(R.id.resaut_status)
    LinearLayout resautStatus;
    
    @BindView(R.id.listview)
    RecyclerView listview;
    
    private EvaluatedListAdapter adapter;
    
    private ArrayList<OrderListBean> list = new ArrayList();
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluated_list);
        ButterKnife.bind(this);
        initView();
        initDate();
    }
    
    private void initDate()
    {
        loadingDate.setVisibility(View.VISIBLE);
        resautStatus.setVisibility(View.GONE);
        Observable<EvaluatedDate> order = RetrofitUtil.getInstance().getApiOfPhp().getEvaluate(
            Integer.parseInt(Singleton.getInstance().getUser().getUser_id()));
        order.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(
            new Observer<EvaluatedDate>()
            {
                @Override
                public void onSubscribe(Disposable d)
                {
                    disposables.add(d);
                }
                
                @Override
                public void onNext(EvaluatedDate pendingPaymentMsg)
                {
                    if (pendingPaymentMsg != null && pendingPaymentMsg.isFlag()
                        && pendingPaymentMsg.getOrderList().size() > 0)
                    {
                        list.clear();
                        list.addAll(pendingPaymentMsg.getOrderList());
                        adapter.notifyDataSetChanged();
                    }
                    else
                        resautStatus.setVisibility(View.VISIBLE);
                }
                
                @Override
                public void onError(Throwable e)
                {
                    loadingDate.setVisibility(View.GONE);
                    resautStatus.setVisibility(View.VISIBLE);
                }
                
                @Override
                public void onComplete()
                {
                    loadingDate.setVisibility(View.GONE);
                }
            });
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == EvaluateGoodsActivity.EVALUTEGOODSACTIVITYCODE && resultCode == RESULT_OK)
        {
            initDate();
        }
    }
    
    /**
     * 监听的回调
     */
    CallBack callBack = new CallBack()
    {
        @Override
        public void doNext(int what, Object obj)
        {
            EvaluateGoodsActivity.startActivity(EvaluatedListActivity.this, (OrderListBean)obj);
        }
    };
    
    private void initView()
    {
        topText.setText("待评价");
        adapter = new EvaluatedListAdapter(this, list, callBack);
        listview.setLayoutManager(new LinearLayoutManager(this));
        listview.setAdapter(adapter);
    }
    
    @OnClick(R.id.back)
    public void onViewClicked()
    {
        finish();
    }
}
